package com.ckjava.xutils.web;

import com.ckjava.xutils.service.CronService;
import com.ckjava.xutils.Constants;
import com.ckjava.xutils.http.HttpResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api
@RestController
@RequestMapping(value = "/cron", produces = "application/json; charset=utf-8")
public class CronController implements Constants {

    @Autowired
    private CronService cronService;

    @RequestMapping(value = "/next", method = RequestMethod.GET)
    @ApiOperation(value = "获取job下一次触发的时间列表")
    public HttpResponse<List<String>> getJobNextFireList(
            @RequestParam(value = "cron") String cron,
            @RequestParam(value = "times", required = false) Integer times) throws Exception {

        if (times == null) {
            times = 10;
        }

        List<String> dateList = new ArrayList<>();
        List<Date> list = cronService.getNextFireDateList(cron, times);
        if (list != null) {
            for (Date date : list) {
                dateList.add(DateFormatUtils.format(date, TIMEFORMAT.DATETIME));
            }
        }
        return HttpResponse.getReturn(dateList, HTTPCODE.code_200, null);
    }
}
